home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BEERSRC.ZIP / BALLER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-10  |  8.3 KB  |  303 lines

  1.  /*-----------------------------------------------------*/
  2. /*                                                       */
  3. /*            T H E   L A S T   E I C H H O F            */
  4. /*                                                       */
  5. /*           [c] copyrigth 1993 by ALPHA-HELIX           */
  6. /*          This module written by Dany Schoch         */
  7. /*                                                       */
  8.  /*-----------------------------------------------------*/
  9.  
  10. #define MAIN_MODULE
  11.  
  12. #ifndef __COMPACT__
  13.    #error This version only supports the COMPACT memory model.
  14. #endif
  15.  
  16. #pragma hdrstop
  17.  
  18. #include <conio.h>
  19. #include <dos.h>
  20. #include <alloc.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <stdarg.h>
  26. #include <errno.h>
  27.  
  28. #include "xmode.h"
  29. #include "fileman.h"
  30. #include "sound.h"
  31. #include "baller.h"
  32.  
  33.  
  34. #define MEMORYREQUIRED        500000L    // Memory used to run.
  35. #define CMDLEN            40    // Max length of command line.
  36.  
  37. static char   cmd[CMDLEN];        // Command line given at startup.
  38. extern unsigned _stklen;                // Programs stacklength.
  39.  
  40.  
  41. // Code.
  42.  
  43. // Error handling routines.
  44.  
  45. void error(char *text, int code, ...)
  46. {
  47.    void powerdown(void);    // Prototype.
  48.    va_list   ap;        // Variable for variable argument list.
  49.  
  50.    va_start(ap, code);
  51.    powerdown();            // Leave graphics, close files, ...
  52.    switch (code) {
  53.       case ENOMEM : printf("%s : NO MORE MEMORY !\n\n", text);
  54.             break;
  55.       case ENOFILE: printf("%s : File '%s' not found !\n\n",
  56.             text, va_arg(ap, char *));
  57.             break;
  58.       case E2BIG  : printf("%s : Out of index table space.\n\n", text);
  59.             break;
  60.       case EINVDAT: printf("%s : Wrong level.\n\n", text);
  61.             break;
  62.       case -1     :
  63.       case EFAULT : printf("James Bond quitting style !!\n\n");
  64.             break;
  65.       default     : perror(text);
  66.    }
  67.    va_end(ap);
  68.    exit(1);
  69. }
  70.  
  71.  
  72. #pragma argsused
  73. int harderror(int errval, int ax, int bp, int si)
  74. {
  75.     return _HARDERR_FAIL;    // Indicate failed DOS function.
  76. }
  77.  
  78.  
  79. /*------------------------------------------------------
  80. Function: powerup & powerdown
  81.  
  82. powerup   : Initializes the system.
  83. powerdown : Undoes the effect of powerup.
  84.         Please note that powerdown should not be
  85.         called within powerup or the sytem will
  86.         hang. So errors encountered in powerup
  87.         can't be handled by the function 'error',
  88.         because this would call powerdown indirectly.
  89. ------------------------------------------------------*/
  90. void powerup(void)
  91. {
  92.  
  93. // Check DOS version.
  94.    if (_osmajor < 3) {
  95.       printf("Try using DOS 3.0 or even a higher version.\n");
  96.       exit(1);
  97.    }
  98.  
  99.    harderr(harderror);            // Install a DOS error handler.
  100.  
  101. // Look for a VGA.
  102.    if (!VGApresent() && !strstr(cmd, "/VGA") && !strstr(cmd, "-VGA")) {
  103.       printf("You need a VGA to run this game.\n");
  104.       exit(1);
  105.    }
  106.  
  107. // Check if there is enough memory available.
  108.    {
  109.       long   mem;
  110.  
  111.       mem = (_SS - _psp)*16 + _stklen;
  112. #ifdef DEBUG
  113.       printf("Program Code+Data : %ld bytes.\n", mem - _stklen);
  114.       printf("Stack size        : %u bytes.\n", _stklen);
  115.       printf("DOS available     : %ld bytes.\n", coreleft());
  116. #endif
  117.       if (coreleft() < MEMORYREQUIRED) {
  118.      printf("This Game requires at least %ld bytes of DOS memory.\n", MEMORYREQUIRED + mem);
  119.      printf("You still need to free up %ld bytes.\n", MEMORYREQUIRED - coreleft());
  120.      exit(1);
  121.       }
  122.    }
  123.  
  124. // Initalize the memory system.
  125.    {
  126.       int   xms;
  127.  
  128.       xms = initfilemanager(40, 512, 8192, error);
  129.       if (xms == -1) abort();        // Suspicious fatal error.
  130.       printf("XMS used      : %d kB.\n", xms);
  131.    }
  132.  
  133. // Looking for SoundBlaster.
  134.    {
  135.       int   io, irq, dma;
  136.       char  *e1, *e2;            // Environment strings.
  137.  
  138.       io = irq = dma = -1;        // Set to 'Autodetect'.
  139.  
  140. // Attempt to read environment variable.
  141.       if ((e1 = getenv("BLASTER")) != NULL) {;
  142. // Duplicate the string so that we don't trash our environment.
  143.      e2 = strdup(e1);
  144. // Now parse the BLASTER variable.
  145.      e1 = strtok(e2, " \t");
  146.      while(e1) {
  147.         switch(toupper(e1[0])) {
  148.            case 'A':        // I/O address.
  149.           io = (int) strtol(e1 + 1, &e1, 16);
  150.           break;
  151.            case 'I':        // Hardware IRQ.
  152.           irq = atoi(e1 + 1);
  153.           break;
  154.            case 'D':        // DMA channel.
  155.           dma = atoi(e1 + 1);
  156.           break;
  157.         }
  158.         e1 = strtok(NULL, " \t");    // Remove blanks.
  159.      }
  160.      free(e2);
  161.       }
  162.       printf("SoundBlaster  : %sfound.\n",
  163.          (initsound(io, irq, dma) ? "" : "not "));
  164.    }
  165.  
  166. // Clear NUM_LOCK, SCROLL_LOCK and CAPS_LOCK.
  167.    outportb(0x60, 0xed);        // Send 'LED' set command.
  168.    delay(10);                // Wait 10ms.
  169.    outportb(0x60, 0);            // Clear all LEDs.
  170.    pokeb(0, 0x417, peekb(0, 0x417) & 0xff8f);
  171.                     // Clear BIOS key states.
  172.  
  173. // Redefine keyboard and timer interrupts.
  174.    int09 = getvect(0x09);
  175.    int08 = getvect(0x08);
  176. // Der naechste befehl ist fast schon ein guter witz.
  177. // Er kopiert die variable 'int08' in eine variable
  178. // im CS. Dies braucht's, damit man locker in 'newint08'
  179. // mit einem 'jmp' zur alten int-routine springen kann.
  180. // Schoen waere es, wenn ich in C sagen koennte, wo ich
  181. // die variablen haben will. Doch das geht leider nicht.
  182. // Oder ich weiss nicht wie??
  183.    copytoCS();
  184.    setvect(0x08, newint08);
  185.    setvect(0x09, newint09);
  186.    setspeed(GAMESPEED);            // Set timer speed.
  187.  
  188. #ifdef DEBUG
  189.    printf("After initialization   : %ld bytes.\n", coreleft());
  190. #endif
  191.  
  192.    printf("\nHit any key to go on.\n");
  193.    waitforkey();
  194.    initxmode();                // Enter graphic mode.
  195.  
  196. }
  197.  
  198. void powerdown(void)
  199. {
  200.    shutxmode();
  201.    setspeed(0);
  202.    setvect(0x09, int09);
  203.    setvect(0x08, int08);
  204.  
  205.    shutsound();
  206.    shutfilemanager();
  207. }
  208.  
  209.  
  210.  
  211. /*------------------------------------------------------
  212. Function: cmdline
  213.  
  214. Description: Do first steps on the command line.
  215. ------------------------------------------------------*/
  216. void cmdline(int argc, char *argv[])
  217. {
  218.    int   i;
  219.    char  *e1;
  220.  
  221. // Concatenate all command strings together in 'cmd'.
  222.    cmd[0] = '\0';            // Clear string.
  223.    for (i = 1; i < argc; i++) {
  224.       if (strlen(cmd)+strlen(argv[i]) < CMDLEN)
  225.      strcat(cmd, argv[i]);
  226.    }
  227.    strupr(cmd);
  228.  
  229. // Help?
  230.    if (strstr(cmd, "/?") || strstr(cmd, "-?")) {    // Help?
  231.       printf("Syntax:   BALLER [options]\n");
  232.       printf("  /vga    Override VGA detection.\n");
  233.       printf("  /ns     Play without sound.\n");
  234.       printf("\n");
  235.       printf("To force SoundBlaster on, use the BLASTER environment variable.\n");
  236.       printf("  e.g. set BLASTER = A220 I7 D1\n");
  237.       printf("\n\n");
  238.       exit(0);                          // Exit nicely.
  239.    }
  240.  
  241. // Get cheat level.
  242.  
  243.    if ((e1 = strstr(cmd, "007.")) == NULL) {
  244.       cheatlevel = 0;            // No cheating this time.
  245.    } else {
  246.       cheatlevel = e1[4] - '0';
  247.       printf("Cheat level:\n");
  248.       if(cheatlevel & CHEATLIFES) printf("    - Unlimited lives\n");
  249.       if(cheatlevel & CHEATMONEY) printf("    - Unlimited money\n");
  250.       if(cheatlevel & CHEATCRASH) printf("    - Eichhof can't be destroyed\n");
  251.       printf("\n");
  252.    }
  253.  
  254. }
  255.  
  256. int main(int argc, char *argv[])
  257. {
  258.  
  259.    clrscr();
  260.    printf("\n\n\nTHE LAST EICHHOF [c] copyright 1993 by ALPHA-HELIX.\n");
  261.    printf("Release 1.1\n\n");
  262.    printf("This game is FREEWARE. Please copy like crazy.\n");
  263.    printf("If you like it, just send a postcard to ALPHA-HELIX\n");
  264.    printf("                                        Rehhalde 18\n");
  265.    printf("                                        6332 Hagendorn\n");
  266.    printf("                                        Switzerland\n");
  267.    printf("\nInternet contact address: tritone@ezinfo.vmsmail.ethz.ch\n\n\n");
  268.  
  269. // Process command line.
  270.    cmdline(argc, argv);
  271.  
  272. // Do initialization.
  273.    powerup();
  274.  
  275. // Load options of last time.
  276.    loadconfig();
  277. // Check command line for 'nosound' option.
  278.    if (strstr(cmd, "/NS") || strstr(cmd, "-NS")) speaker(0);
  279.  
  280. // Open date bases.
  281.    datapool = opendatabase("beer.dat");
  282.  
  283. #ifdef DEBUG
  284.    screenmode(2);
  285.    printf("Memory available for data : %ld bytes.\n", coreleft());
  286.    waitforkey();
  287.    setxmode();
  288.    setstandardpalette();
  289. #endif
  290.  
  291.    intro();                // Show Blick intro.
  292.  
  293.    windowy1 = BARY;            // Game window y-Size.
  294.  
  295.    menu();
  296.  
  297.    closedatabase(datapool);
  298.  
  299. // Going down and return to OS.
  300.    powerdown();
  301.    return 0;
  302. }
  303.